from scrapy.spider import BaseSpider
from scrapy.selector import Selector

from spider.items import SpiderItem
import MySQLdb


class BitsuperuperSpider(BaseSpider):

    name = "bitsuper"

    conn = MySQLdb.connect(host="localhost",user="spider",passwd="Xjk@4047218?",db="spider")
    cursor = conn.cursor()
    sql = "select url,maxpages from sp_ba_carmodel order by id asc"
    result = cursor.execute(sql)
    priceurl = []
    for row in cursor.fetchall():
        url =  row[0]+"baojia/c0/?bizmode=1"
        if row[1] > 1:
            for j in range(1,row[1]+1):
                priceurl.append(url+"&page="+str(j))
        else:
            priceurl.append(url)

    cursor.close()
    #start_urls = ["http://car.bitauto.com/aodia3-3999/m104780/baojia/c0/?bizmode=1&page=1#V"]
    #print start_urls
    start_urls = priceurl


    def parse(self,response):
        sel = Selector(response)
        temp = response.url.split("/")[-1].split("-")        

        areas = sel.xpath('//div[@class="main_alldeler"]//div[@class="list_deler"]')
        items = []       
        for area in areas:
            item = SpiderItem()
            item["carmodelurl"] = response.url.split("/")[4]
            #item["carmodelurl"] = response.url
            item["carshop"] = area.xpath('.//div[@class="fist_t"]//h4[1]/a/text()').extract()
            item["price"] = area.xpath('.//div[@class="jiage_t"]//strong//a/text()').extract()
            item["city"] = area.xpath('.//div[@class="jiage_t"]/span[last()]/text()').extract()
            item["type"] = "bitprice"
            items.append(item)
        return items

